package com.shy.controller;

import com.github.pagehelper.PageInfo;
import com.shy.config.model.R;
import com.shy.config.model.SecurityUser;
import com.shy.config.util.AuthUtils;
import com.shy.entity.User;
import com.shy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "后台用户管理")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/login/info")
    public R<SecurityUser> loadLoginUserInfo() {
        // 获取当前登录用户信息
        //User user = userService.queryUserById(AuthUtils.getLoginSecurityUserId());
        return R.success(AuthUtils.getLoginSecurityUser());
    }

    @ApiOperation("是否记住我")
    @GetMapping("isLogin")
    public R<String> isLogin() {
        return R.success(null);
    }

    @ApiOperation("用户资料详情")
    @GetMapping("/detail")
    public R<User> loadUserDetail() {
        // 获取当前登录用户信息
        User user = userService.queryUserDetailById(AuthUtils.getLoginSecurityUserId());
        return R.success(user);
    }

    @ApiOperation("查询旧密码是否一致")
    @GetMapping("/checkOldPwd")
    public R<String> checkOldPwd(@RequestParam String oldPwd) {
        // 查询旧密码是否一致
        Boolean checkOldPwd = userService.checkLoginPwd(oldPwd);
        return checkOldPwd ? R.success(null) : R.failure("密码错误");
    }

    @ApiOperation("修改密码")
    @PutMapping("/updatePwd")
    public R<String> updatePwd(@RequestBody User user) {
        // 修改密码
        Boolean modifyPwd = userService.modifyPwd(user);
        return modifyPwd ? R.success(null) : R.failure("修改密码失败");
    }

    @ApiOperation("分页查询用户")
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/pageList")
    public R<PageInfo<User>> queryUserList(@RequestParam Integer current,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.queryUserListByPage(current, pageSize);
        return R.success(pageInfo);
    }

    @ApiOperation("用户资料详情")
    @PreAuthorize("hasAuthority('user:view')")
    @GetMapping("/detail/{userId}")
    public R<User> loadUserDetailById(@PathVariable Integer userId) {
        // 获取当前用户详情
        User user = userService.queryUserDetailById(userId);
        return R.success(user);
    }


    @ApiOperation("修改用户资料")
    @PreAuthorize("hasAuthority('user:edit')")
    @PutMapping("/editUser")
    public R<User> editUser(@RequestBody User user) {
        // 修改用户资料
        Boolean modifyUser = userService.modifyUser(user);
        //重新获取用户信息
        User editilUser = userService.queryUserDetailById(user.getId());
        return modifyUser ? R.success(editilUser) : R.failure("修改用户资料失败");
    }
   /* @PreAuthorize("hasAuthority('user:delete')")
    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public R<String> deleteUser(@PathVariable Integer id) {
        // 删除用户
        Boolean deleteUser = userService.deleteUserById(id);
        return deleteUser ? R.success(null) : R.failure("删除用户失败");
    }*/
    @ApiOperation("添加用户")
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping("/addUser")
    public R<User> addUser(@RequestBody User user) {
        // 添加用户
        Boolean addUser = userService.addUser(user);
        return addUser ? R.success(null) : R.failure("添加用户失败");
    }
    @ApiOperation("批量删除")
    @PreAuthorize("hasAuthority('user:delete')")
    @RequestMapping("/batchDelete/{userIds}")
    public R<String> batchDelete(@PathVariable List<Integer> userIds) {
        // 批量删除
        Boolean batchDelete = userService.batchDelete(userIds);
        return batchDelete?R.success(null):R.failure("批量删除失败");
    }
}
